package com.iflysse.services.impl;

import com.iflysse.dao.KnowledgePointDao;
import com.iflysse.pojo.bo.req.KnowledgePointBoReq;
import com.iflysse.pojo.bo.resp.KnowledgePointBoResp;
import com.iflysse.pojo.bo.resp.KnowledgePointListBoResp;
import com.iflysse.pojo.po.req.KnowledgePointPoReq;
import com.iflysse.pojo.po.resp.KnowledgePointPoResp;
import com.iflysse.services.KnowledgePointService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class KnowledgePointServiceImpl implements KnowledgePointService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    KnowledgePointDao knowledgePointDao ;
    /**
     * 获取第一级知识点
     *
     * @return
     */
    @Override
    public KnowledgePointListBoResp getRootKnowledgePointList() throws SQLException {
        String parentId ="00000000-0000-0000-0000-000000000000";
        KnowledgePointBoReq knowledgePointBoReq = new KnowledgePointBoReq();
        knowledgePointBoReq.setKnowledgePointId(parentId);
        return getKnowledgePointListByParentId(knowledgePointBoReq);
    }

    /**
     * 根据知识点父节点获取子节点
     *
     * @param knowledgePointBoReq
     * @return
     */
    @Override
    public KnowledgePointListBoResp getKnowledgePointListByParentId(KnowledgePointBoReq knowledgePointBoReq) throws SQLException {
        KnowledgePointPoReq knowledgePointPoReq = CommonUtil.objToObj(knowledgePointBoReq, KnowledgePointPoReq.class);
        knowledgePointPoReq.setKnowledegPointId(knowledgePointBoReq.getKnowledgePointId());
        //根据父节点获取子节点
        List<KnowledgePointPoResp> knowledgePointsList = knowledgePointDao.getKnowledgePointsByParentId(knowledgePointPoReq);
        KnowledgePointListBoResp knowledgePointListBoResp = new KnowledgePointListBoResp();
        knowledgePointListBoResp.setKnowledgePointBoRespList(new ArrayList<>());
        //将po转bo
        knowledgePointsList.forEach(knowledgePointPoResp -> {
            KnowledgePointBoResp knowledgePointBoResp = new KnowledgePointBoResp();
            knowledgePointBoResp.setKnowledgePointId(knowledgePointPoResp.getKnowledgePointId());
            knowledgePointBoResp.setKnowledgePointParentId(knowledgePointPoResp.getKnowledgePointParentId());
            knowledgePointBoResp.setKnowledgePointName(knowledgePointPoResp.getKnowledgePointName());
            knowledgePointBoResp.setLevel(knowledgePointPoResp.getLevel());
            knowledgePointBoResp.setOrderIndex(knowledgePointPoResp.getOrderIndex());
            knowledgePointListBoResp.getKnowledgePointBoRespList().add(knowledgePointBoResp);
        });
        return knowledgePointListBoResp;
    }
}
